<?php
session_start();

// 检查用户是否已登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit();
}
require_once 'track_online.php';
// 当前用户信息
$currentUser = $_SESSION['username'];
$userDir = __DIR__ . "/user/$currentUser";

// 确保用户文件夹存在
if (!is_dir($userDir)) {
    mkdir($userDir, 0777, true);
}

// 用户信息文件路径
$infoFile = "$userDir/info.json";
if (!file_exists($infoFile)) {
    $defaultData = [
        'bio' => '没有填写介绍',
        'gender' => '未设置'
    ];
    file_put_contents($infoFile, json_encode($defaultData));
}

// 加载用户信息
$userInfo = json_decode(file_get_contents($infoFile), true);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的 - JIILCHAT</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
            -webkit-tap-highlight-color: transparent;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
            min-height: 100vh;
        }
        
        /* 顶部导航栏 - 微信风格 */
        .navbar {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background: #f0f2f5;
            color: #333;
            padding: 15px 20px;
            display: flex;
            align-items: center;
            box-shadow: 0 1px 4px rgba(0,0,0,0.05);
            z-index: 100;
            border-bottom: 1px solid #e0e4e9;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: #555;
            font-size: 20px;
            margin-right: 15px;
            cursor: pointer;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: #e4e6eb;
        }
        
        .navbar-title {
            font-size: 18px;
            font-weight: 600;
            flex-grow: 1;
            color: #333;
        }
        
        /* 主内容区 */
        .content {
            margin-top: 70px; /* 导航栏高度 */
            padding: 0;
            width: 100%;
        }
        
        /* 个人信息卡片 - 微信风格 */
        .profile-card {
            background: white;
            padding: 20px;
            border-bottom: 1px solid #f0f2f5;
        }
        
        .profile-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .user-avatar {
            width: 80px;
            height: 80px;
            border-radius: 8px;
            background: #e0e4e9;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }
        
        .user-avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .user-details {
            flex-grow: 1;
        }
        
        .username {
            font-size: 22px;
            font-weight: 600;
            color: #333;
            margin-bottom: 8px;
        }
        
        .user-meta {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 8px;
        }
        
        .gender {
            display: flex;
            align-items: center;
            font-size: 15px;
            color: #7f8c8d;
        }
        
        .gender.male {
            color: #3498db;
        }
        
        .gender.female {
            color: #e74c3c;
        }
        
        .gender-icon {
            margin-right: 5px;
        }
        
        .bio {
            font-size: 15px;
            color: #7f8c8d;
            line-height: 1.5;
        }
        
        /* 二维码按钮样式 */
        .qr-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #f0f2f5;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .qr-btn:hover {
            background: #e4e6eb;
            transform: scale(1.05);
        }
        
        .qr-btn i {
            font-size: 20px;
            color: #7f8c8d;
        }
        
        /* 功能列表 - 微信风格 */
        .menu-section {
            background: white;
            margin-top: 15px;
            border-top: 1px solid #f0f2f5;
            border-bottom: 1px solid #f0f2f5;
        }
        
        .menu-item {
            display: flex;
            align-items: center;
            padding: 16px 20px;
            border-bottom: 1px solid #f0f2f5;
            text-decoration: none;
            color: #333;
            transition: background 0.2s;
            cursor: pointer;
        }
        
        .menu-item:last-child {
            border-bottom: none;
        }
        
        .menu-item:hover {
            background-color: #f8f9fa;
        }
        
        .menu-icon {
            width: 28px;
            height: 28px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
            font-size: 20px;
            color: #2d3748;
        }
        
        .menu-text {
            flex-grow: 1;
            font-size: 17px;
            color: #333;
        }
        
        .menu-arrow {
            color: #c0c4cc;
            font-size: 16px;
        }
        
        /* 底部空间 */
        .footer-space {
            height: 30px;
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .navbar {
                padding: 12px 15px;
            }
            
            .back-btn {
                font-size: 18px;
                width: 32px;
                height: 32px;
            }
            
            .navbar-title {
                font-size: 16px;
            }
            
            .user-avatar {
                width: 70px;
                height: 70px;
            }
            
            .username {
                font-size: 20px;
            }
            
            .menu-item {
                padding: 14px 16px;
            }
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="navbar">
        <button class="back-btn" onclick="location.href='index.php'">
            <i class="fas fa-arrow-left"></i>
        </button>
        <div class="navbar-title">我的资料</div>
    </div>
    
    <!-- 主内容区 -->
    <div class="content">
        <!-- 个人信息卡片 -->
        <div class="profile-card">
            <div class="profile-info">
                <div class="user-avatar">
                    <?php if (!empty($userInfo['avatar'])): ?>
                        <img src="<?= htmlspecialchars($userInfo['avatar']) ?>" alt="头像">
                    <?php else: ?>
                        <i class="fas fa-user" style="font-size: 36px; color: #7f8c8d;"></i>
                    <?php endif; ?>
                </div>
                <div class="user-details">
                    <div class="username"><?= htmlspecialchars($currentUser) ?></div>
                    
                    <!-- 显示性别 -->
                    <?php if (isset($userInfo['gender'])): ?>
                        <div class="gender <?= $userInfo['gender'] === '男' ? 'male' : ($userInfo['gender'] === '女' ? 'female' : '') ?>">
                            <i class="gender-icon fas <?= $userInfo['gender'] === '男' ? 'fa-mars' : ($userInfo['gender'] === '女' ? 'fa-venus' : 'fa-question') ?>"></i>
                            <?= htmlspecialchars($userInfo['gender']) ?>
                        </div>
                    <?php endif; ?>
                    
                    <!-- 显示个人介绍 -->
                    <div class="bio"><?= htmlspecialchars($userInfo['bio']) ?></div>
                </div>
                
                <!-- 二维码按钮 -->
                <div class="qr-btn" onclick="viewProfile()">
                    <i class="fas fa-qrcode"></i>
                </div>
            </div>
        </div>
        
        <!-- 功能列表 -->
        <div class="menu-section">
            <div class="menu-item" onclick="location.href='user_settings.php'">
                <div class="menu-icon">
                    <i class="fas fa-user-cog"></i>
                </div>
                <div class="menu-text">用户设置</div>
                <div class="menu-arrow">
                    <i class="fas fa-chevron-right"></i>
                </div>
            </div>
            
            <div class="menu-item" onclick="location.href='dieacc.php'">
                <div class="menu-icon">
                    <i class="fas fa-cog"></i>
                </div>
                <div class="menu-text">系统设置</div>
                <div class="menu-arrow">
                    <i class="fas fa-chevron-right"></i>
                </div>
            </div>
           <div class="menu-item" onclick="location.href='ch.php'">
    <div class="menu-icon">
        <i class="fas fa-crown"></i> <!-- 使用皇冠图标 -->
    </div>
    <div class="menu-text">我的称号</div>
    <div class="menu-arrow">
        <i class="fas fa-chevron-right"></i>
    </div>
</div>
        </div>
        
        <!-- 设置区域 -->
        <div class="menu-section">
            <div class="menu-item" onclick="location.href='about.php'">
                <div class="menu-icon">
                    <i class="fas fa-info-circle"></i>
                </div>
                <div class="menu-text">关于</div>
                <div class="menu-arrow">
                    <i class="fas fa-chevron-right"></i>
                </div>
            </div>
            
            <div class="menu-item" onclick="location.href='qlinfo.php?group_name=反馈群'">
                <div class="menu-icon">
                    <i class="fas fa-comments"></i>
                </div>
                <div class="menu-text">加入反馈群</div>
                <div class="menu-arrow">
                    <i class="fas fa-chevron-right"></i>
                </div>
            </div>
        </div>
        
        <div class="footer-space"></div>
    </div>
    
    <script>
        // 查看个人资料
        function viewProfile() {
            const username = "<?= $currentUser ?>";
            window.location.href = `userinfo.php?user=${encodeURIComponent(username)}`;
        }
    </script>
</body>
</html>